package com.visionblue.business.ship.page.register.config;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;

import com.visionblue.business.ship.App;

/**
 * @author: tekabo
 * @date : 2021/4/2117:06
 * desc:
 */
public class PackageInfoGet {
    public static App applicationContext;

    public static int versionCode;//当前app版本号
    public static String versionName;//当前app版本名称
    public static String appName;//当前app的名称
    public static String appPackage;//当前app包名

    public PackageInfoGet(App application){
        PackageInfoGet.applicationContext = application;

        //获取当前软件信息
        initPackageInfo();
    }


    private static void initPackageInfo() {
        // 获取packagemanager的实例
        PackageManager packageManager = applicationContext.getPackageManager();
        // getPackageName()是你当前类的包名，0代表是获取版本信息
        PackageInfo packInfo = null;
        try {
            packInfo = packageManager.getPackageInfo(applicationContext.getPackageName(), PackageManager.GET_SIGNATURES);
            versionCode = packInfo.versionCode;
            versionName = packInfo.versionName;
            appName = packInfo.applicationInfo.loadLabel(packageManager).toString();
            appPackage = applicationContext.getPackageName();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 返回当前程序版本号
     */
    public static synchronized String getAppVersionCode(Context context) {
        int versionCode = 0;
        try {
            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
            versionCode = pi.versionCode;
        } catch (Exception e) {

        }
        return versionCode + "";
    }


    /**
     * 返回当前程序版本名
     */
    public static synchronized String getAppVersionName(Context context) {
        String versionName = null;
        try {
            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
            versionName = pi.versionName;
        } catch (Exception e) {
            Log.e("VersionInfo", "Exception", e);
        }
        return versionName;
    }

}
